// app.js

import {
  requestGet,
  requestPost,
  requestPut,
  requestDelete
} from './utils/request.js'
App({
  onLaunch() {
    //初始化音频管理
    this.globalData.audioManager = wx.createInnerAudioContext({
      useWebAudioImplement: true
    })

    /**
     * 封装的request
     */
    this.$http = {
      get: requestGet,
      post: requestPost,
      put: requestPut,
      delete: requestDelete
    }
  },
  // // 获取用户头像 昵称
  // getUserProfile(e) {
  //   console.log('hahaha')
  //   let that = this
  //   that.globalData.actionSheetShow = false
  //   // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
  //   wx.getUserProfile({
  //     desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
  //     success: (res) => {
  //       console.log(res.userInfo,res, 'res.userInfo')
  //       that.globalData.userInfo = res.userInfo
  //       that.globalData.hasUserInfo = true
  //       that.globalData.iv = res.iv
  //       that.globalData.encryptedData = res.encryptedData
  //       that.globalData.popup = true // 获取用户手机弹窗
  //       that.getWxToken()  // 获取微信token
  //     },
  //     fail: (err) => {
  //       console.log('09876')
  //       that.globalData.actionSheetShow = true
  //     }
  //   })
  // },

  // // 获取微信token
  // getWxToken: function() {
  //   let that = this
  //   let url = `${that.globalData.baseUrl}/api/getWxToken`
  //   wx.request({
  //     url: url,
  //     method: 'GET',
  //     success: (res) => {
  //       if(res.data.code === 0) {
  //         that.globalData.accessToken = res.data.accessToken
  //       }
  //     },
  //     fail: (error) => {
  //       console.log(error)
  //     }
  //   })
  // },
  // // 获取手机号码
  // getPhoneNumbers: function(e) {
  //   let that = this
  //   let ACCESS_TOKEN = that.globalData.accessToken
  //   that.globalData.popup = false
  //   let url = `https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=${ACCESS_TOKEN}`
  //   wx.request({
  //     url: url,
  //     method: 'POST',
  //     data: {
  //       code: e
  //     },
  //     success: function(res) {
  //       if(res.data.errcode === 0) {
  //         let phoneNum = res.data.phone_info.phoneNumber.slice(0,3) + "****" + res.data.phone_info.phoneNumber.slice(7,11)
  //         that.globalData.phoneNum = phoneNum
  //         // 调用登录接口
  //         that.login(res.data.phone_info.phoneNumber)
  //       } else {
  //         wx.showToast({
  //           title: '失败',
  //           icon: 'error',
  //           duration: 2000
  //         })
  //       }
  //     },
  //     fail: (err) => {
  //       that.globalData.popup = true
  //     }
  //   })
  // },

  // // 登录接口调用
  // login(phoneNumber) {
  //   let that = this
  //   let url = `${that.globalData.baseUrl}/api/login`
  //   wx.showLoading({
  //     title: '加载中',
  //   })
  //   setTimeout(function () {
  //     wx.hideLoading()
  //   }, 3000)
  //   wx.request({
  //     url: url,
  //     method: 'POST',
  //     data: {
  //       code: that.globalData.code,
  //       encryptedData:that.globalData.encryptedData,//用户敏感信息
  //       iv:that.globalData.iv,//解密算法的向量
  //       phoneNumber: phoneNumber
  //     },
  //     success: function(result) {
  //       if(result.data.code === 0) {
  //         that.globalData.token = result.data.token
  //         wx.showToast({
  //           title: '授权成功',
  //           icon: 'success',
  //           duration: 3000
  //         })
  //         if(result.data.token) {
  //           wx.setStorageSync('token', result.data.token) 
  //         }
  //       }
  //     },
  //     fail: function(err) {
  //       wx.showToast({
  //         title: '授权失败，请联系区慧勤务中心',
  //         icon: 'error',
  //         duration: 2000
  //       })
  //     }
  //   })
  // },
  // 权限询问
  getRecordAuth: function () {
    wx.getSetting({
      success(res) {
        console.log("succ")
        console.log(res)
        if (!res.authSetting['scope.record']) {
          wx.authorize({
            scope: 'scope.record',
            success() {
              // 用户已经同意小程序使用录音功能，后续调用 wx.startRecord 接口不会弹窗询问
              console.log("succ auth")
            },
            fail() {
              console.log("fail auth")
            }
          })
        } else {
          console.log("record has been authed")
        }
      },
      fail(res) {
        console.log("fail")
        console.log(res)
      }
    })
  },
  // globalData数据监听
  watchActionSheetShow(method) {
    var obj = this.globalData
    Object.defineProperty(obj,'actionSheetShow',{
      configurable: true,
      enumerable: true,
      set:function(value) {
        this._actionSheetShow = value
        console.log('是否会被执行actionSheetShow')
        method(value)
      },
      get: function() {
        console.log('调用actionSheetShow')
        return this._actionSheetShow
      }
    })
  },

  watchPopup(method) {
    var obj = this.globalData
    Object.defineProperty(obj,'popup',{
      configurable: true,
      enumerable: true,
      set:function(value) {
        this._popup = value
        console.log('是否会被执行popup')
        method(value)
      },
      get: function() {
        console.log('调用popup')
        return this._popup
      }
    })
  },

  watchUserInfo(method) {
    var obj = this.globalData
    Object.defineProperty(obj,'userInfo',{
      configurable: true,
      enumerable: true,
      set:function(value) {
        this._userInfo = value
        console.log('是否会被执行userInfo')
        method(value)
      },
      get: function() {
        console.log('调用userInfo')
        return this._userInfo
      }
    })
  },

  watchPhoneNum(method) {
    var obj = this.globalData
    Object.defineProperty(obj,'phoneNum',{
      configurable: true,
      enumerable: true,
      set:function(value) {
        this._phoneNum = value
        console.log('是否会被执行phoneNum')
        method(value)
      },
      get: function() {
        console.log('调用phoneNum')
        return this._phoneNum
      }
    })
  },
  /**
   * 全局数据
   */
  globalData: {
    userInfo: {},
    code: '', // 微信登录获取的code
    actionSheetShow: false,
    hasUserInfo: false,
    iv: '',
    encryptedData: '',
    popup: false,
    accessToken: '',
    phoneNum: '',
    token: '',
    // baseUrl: 'http://328732ug78.qicp.vip',
    baseUrl: 'http://171.217.24.24:8081',
    // baseUrl: 'http://cdgx-cyhqw.rpaii.com:8081',
    audioManager: null //全局音频
  },
})